package com.qf.com.qf.grass.task;

import com.qf.com.qf.grass.service.GrassService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author zdl
 * @date 2025/3/31
 */
//@Component
public class GrassTask {

    @Resource
    private RedissonClient redissonClient;

    @Resource
    private GrassService grassService;

    // 每日凌晨1点执行
    //@Scheduled(cron = "0 0 1 * * ?")
    //@Scheduled(cron = "0/10 * * * * ?")
    @Async
    public void updateGrassLikeNum(){
        RLock lock = redissonClient.getLock("grass:likenum");
        try {
            if (lock.tryLock(0,0, TimeUnit.SECONDS)){
                // 业务逻辑
                grassService.batchUpdateLikeNum();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (lock.isHeldByCurrentThread() && lock.isLocked()){
                lock.unlock();
            }
        }
    }
}
